﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Collector.Device
{
    class PulseApplicantion : ICloneable
    {
        // 实时压力采样点，最大队列数量 @c mPluseDataMap Queue<double>
        public const int QUEUE_PULSE_DATA_MAXIMUM = 10;

        // 三部部位Index映射 ThreeBodyPartIndexMap
        static public readonly Dictionary<int, BodyPart> TBPIndexMap = new Dictionary<int, BodyPart>
        {
            {0, BodyPart.eBODY_PART_CUN},
            {1, BodyPart.eBODY_PART_GUAN},
            {2, BodyPart.eBODY_PART_CHI}
        };

        // 脉象实时压力采样点，只用于实时显示脉压值
        private Dictionary<BodyPart, Queue<double>> mPluseDataMap = new Dictionary<BodyPart, Queue<double>>
        {
            {BodyPart.eBODY_PART_CUN, new Queue<double>()},
            {BodyPart.eBODY_PART_GUAN, new Queue<double>()},
            {BodyPart.eBODY_PART_CHI, new Queue<double>()}
        };

        // 标记当前脉压sensor的类型
        private byte mSensorType = 0;

        public Dictionary<BodyPart, Queue<double>> PluseData
        {
            get { return mPluseDataMap; }
        }

        public bool pushPulseData(byte[] frame, ProductType type)
        {
            byte payload_size = frame[IDeviceProtocolShape.FRAME_FIELD_LENGTH_OFFSET];
            int end_pos = 4 + (int)payload_size;
            
            if (mSensorType == 0)
            {
                // 获取SENSOR类型信息
                mSensorType = frame[end_pos - 2];
            }

            if (type == ProductType.ePRODUCT_TYPE_SINGLE)
            {
                int d = (frame[IDeviceProtocolShape.FRAME_FIELD_PULSE_DATA_L]) | 
                        (frame[IDeviceProtocolShape.FRAME_FIELD_PULSE_DATA_M] << 8) | 
                        (frame[IDeviceProtocolShape.FRAME_FIELD_PULSE_DATA_H] << 16);
                // 除以100是Pa转化为mbar单位，除以((version - 2) * 31 + 1)是因为：
                // if version == 2:  ==>   HP系数为1
                // if version == 3:  ==>   ZG系数为32
                // if version == 4:  ==>   LINK系数为1
                // 如果版本信息为0 表示为原来版本，统一系数为1
                // 目前只有ZG是32, 其他都为1
                double ratio = mSensorType == 3 ? 32 : 1;
                // mBar / 100
                double pPData = d / 100.0 / ratio;

                if (mPluseDataMap[BodyPart.eBODY_PART_CUN].Count >= QUEUE_PULSE_DATA_MAXIMUM)
                    mPluseDataMap[BodyPart.eBODY_PART_CUN].Dequeue();
                mPluseDataMap[BodyPart.eBODY_PART_CUN].Enqueue(pPData);                
            }
            else if (type == ProductType.ePRODUCT_TYPE_THREE)
            {
                for(int i=0; i<3; i++)
                {

                    int d = (frame[IDeviceProtocolShape.FRAME_FIELD_PULSE_DATA_L + i*3]) |
                            (frame[IDeviceProtocolShape.FRAME_FIELD_PULSE_DATA_M + i*3] << 8) |
                            (frame[IDeviceProtocolShape.FRAME_FIELD_PULSE_DATA_H + i*3] << 16);
                    double ratio = mSensorType == 3 ? 32 : 1;
                    // mBar / 100
                    double pPData = d / 100.0 / ratio;

                    var part = TBPIndexMap[i];
                    if (mPluseDataMap[part].Count >= QUEUE_PULSE_DATA_MAXIMUM)
                        mPluseDataMap[part].Dequeue();
                    mPluseDataMap[part].Enqueue(pPData);
                }
            } 
            else
            {
                return false;
            }

            return true;
        }

        public object Clone()
        {
            PulseApplicantion newObj = new PulseApplicantion();
            newObj.mPluseDataMap = new Dictionary<BodyPart, Queue<double>>(mPluseDataMap);


            return newObj;
        }
    }
}
